package com.kgc.web;

import com.kgc.dao.GonggaoDao;
import com.kgc.entity.Gonggao;
import com.kgc.untils.PageUtils;
import com.kgc.vo.R;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@WebServlet(name = "gonggaoServlet", value = "/gonggaoServlet")
public class gonggaoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String opt = request.getParameter("opt");
        GonggaoDao gonggaoDao = new GonggaoDao();
        if ("list".equals(opt)) {   // 查所有
            String name = request.getParameter("name");
            String page = request.getParameter("page");
            String limit = request.getParameter("limit");
            PageUtils pageUtils = new PageUtils();
            if (StringUtils.isNotEmpty(page)) {
                pageUtils.setCurrPageNo(Integer.parseInt(page));
            }
            if (StringUtils.isNotEmpty(limit)) {
                pageUtils.setPageSize(Integer.parseInt(limit));
            }
            int count = gonggaoDao.countAll(name);
            pageUtils.init(count);
            //查找数据
            List<Gonggao> gonggaoList = gonggaoDao.selectAll(pageUtils, name);
            response.getWriter().write(R.success("公告加载成功", count, gonggaoList).toJSON());
        } else if ("add".equals(opt)) {  // 添加
            Date date = new Date();
            long time = date.getTime();
            Timestamp timestamp = new Timestamp(time);
            String title = request.getParameter("title");
            String content = request.getParameter("content");
            String status = request.getParameter("status");
            int count = gonggaoDao.addGonggao(new Gonggao(title, content, 1, timestamp, timestamp, Integer.parseInt(status)));
            if (count > 0) {
                if (count > 0) {
                    response.getWriter().write(R.success("公告添加成功!").toJSON());
                } else {
                    response.getWriter().write(R.error("公告添加失败!").toJSON());
                }
            }
        } else if ("edit".equals(opt)) {
            Date date = new Date();
            long time = date.getTime();
            Timestamp timestamp = new Timestamp(time);
            String id = request.getParameter("id");
            String title = request.getParameter("title");
            String content = request.getParameter("content");
            String status = request.getParameter("status");
            int count = gonggaoDao.updateGonggao(new Gonggao(Integer.parseInt(id), title, content, 1, timestamp, Integer.parseInt(status)));
            if (count > 0) {
                if (count > 0) {
                    response.getWriter().write(R.success("公告修改成功!").toJSON());
                } else {
                    response.getWriter().write(R.error("公告修改失败!").toJSON());
                }
            }
        } else if ("delete".equals(opt)) {
            String id = request.getParameter("id");
            int count = gonggaoDao.deleteGonggao(id);
            if (count > 0) {
                if (count > 0) {
                    response.getWriter().write(R.success("公告删除成功!").toJSON());
                } else {
                    response.getWriter().write(R.error("公告删除失败!").toJSON());
                }
            }
        }

    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doGet(request, response);
    }
}
